package batch.process.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class BatchProcessUtils {
	
	private static BatchProcessLogger logger = new BatchProcessLogger();
	
	public static void loadProperties() throws Exception {				
		List propPaths = BatchProcessProperties.getList(Constant.MAIN_PROPERTIES_FILE_PATH_STR);
			Iterator<String> it = propPaths.iterator();
			while (it.hasNext()) {
				String path = it.next();				
				logger.info("Reading property file " + path + " ... ",	BatchProcessUtils.class);
				BatchProcessProperties.load(path);
			}
		propPaths = null;
	}
	
	@SuppressWarnings("deprecation")
	public static boolean isWeekend(Date date) {		
		boolean result = false;
		try {
			Calendar calendar = Calendar.getInstance();   
			calendar.setTime(date);
			if ((calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || 
					(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)){
				result = true;
			}			
			
		} catch (Exception e) {
			logger.error(" Error checking if the given date is a weekend day : " + e, 
					BatchProcessUtils.class);
			e.printStackTrace();
		}
		return(result);
	}


	public static void setLastUpdateFile(String filePath, String fileName, Date lastUpdate) {		   
	   try {
		    File file = new File(filePath + fileName);		    
		    if (file != null && file.exists()) {
		    	file.delete();
		    } else {
			    file = new File(filePath);
			    if (file == null || !file.exists()) {
			    	file.mkdir();
			    }
		    }
            FileOutputStream fs = new FileOutputStream(filePath + fileName);   
            ObjectOutputStream os = new ObjectOutputStream(fs);   
            os.writeObject(lastUpdate);   
            os.close();            
        } catch (Exception e) {   
        	logger.error(" Error saving last update file : " + 
        			filePath + "/" + fileName + " " + e, BatchProcessUtils.class);
        	e.printStackTrace();   
        }   
	}
		
	public static Date getLastUpdateFile(String filePath, String fileName) {
		File file = new File(filePath + fileName);
		Date result = new Date();
		try {
			if (file !=null && file.exists()) {
	            FileInputStream fs = new FileInputStream(file);	            		   
	            ObjectInputStream os = new ObjectInputStream(fs);   
	            result = (Date)os.readObject();   
	            os.close();   
			} 
		} catch (Exception e) {
			logger.error(" Error acquiring last update file : " + 
					filePath + "/" + fileName + " " + e, BatchProcessUtils.class);
		}
		return(result);
	}

	
	public static Date dateConvert(SimpleDateFormat format, String dateStr) {
	    if(dateStr == null || 
	    		dateStr.trim().equals("")) {
	      return(null);
	    }
	    
	    try {
	      return(format.parse(dateStr));
	    } catch(Exception e) {
	    	logger.error(" Error conveting date : " + dateStr + " "
					+ e, BatchProcessUtils.class);
	    	throw new RuntimeException(e);
	    }
	}

	public static String dateConvert(SimpleDateFormat format, Date date) {
	    if(date == null) {
	      return("");
	    }
	    
	    try {
	      return(format.format(date));
	    } catch(Exception e) {
	    	logger.error(" Error conveting date : " + date.toString() + " "
					+ e, BatchProcessUtils.class);
	    	throw new RuntimeException(e);
	    }
	}
	
	public static int dateCompare (Date date1, Date date2, SimpleDateFormat dateFormat) {
		int result = -2;
		try {
			Date formatedDate1 = BatchProcessUtils.dateConvert(dateFormat, BatchProcessUtils.dateConvert(dateFormat,date1));
			Date formatedDate2 = BatchProcessUtils.dateConvert(dateFormat,BatchProcessUtils.dateConvert(dateFormat,date2));
	
			if (formatedDate1.after(formatedDate2)) {
				result =  1;
			} else if (formatedDate1.before(formatedDate2)) {
				result =  -1;
			} else if (formatedDate1.equals(formatedDate2)) {
				result =  0;
			}	
			
		} catch (Exception e) {
			logger.error(" Error comparing dates : " + e, BatchProcessUtils.class);
		}
		return result;
		
	}
	
	public static void createTempDir(String filePath) {		   
		   try {
			    File file = new File(filePath);		    
			    if (file == null || !file.exists()) {
			    	file.mkdir();
			    }
	        } catch (Exception e) {   
	        	logger.error(" Error creating temporary directory : " + 
	        			filePath + " " + e, BatchProcessUtils.class);
	        	e.printStackTrace();   
	        }   
		}

}
